package com.huike.ycu.mybatis.web;

import com.huike.ycu.mybatis.model.UmsAdmin;
import com.huike.ycu.mybatis.result.Result;
import com.huike.ycu.mybatis.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;


    @RequestMapping("/")
    public String index01(){
        return "redirect:/cs";
    }
    @RequestMapping("/cs")
    public String index02(){
        return "user/login";
    }

    @RequestMapping("/index")
    /*@ResponseBody*/
    public String index(){
                return "Hello,欢迎";
    }

    @RequestMapping("/login")
    public String login(Model model,String username,String password){
        ModelAndView mav = new ModelAndView("templates/user/login");

        UmsAdmin admin = new UmsAdmin();
        admin.setUsername(username);
        admin.setPassword(password);

        UmsAdmin dbAdmin = adminService.login(admin);

        if(ObjectUtils.isEmpty(dbAdmin)){
            mav.setStatus(HttpStatus.OK);
            mav.addObject("msg","该用户不存在");
            return mav.toString();
        }
        if(password.equals( dbAdmin.getPassword() ) ) {
            mav.addObject("admin", dbAdmin);
        }
         return "mainpage/index";


    }
    @RequestMapping("/toRegister")
    public String toRegister(Model model){
        return "user/register";
    }
    @RequestMapping("/register")
    public String register(Model model,String username,String password){
        UmsAdmin admin = new UmsAdmin();
        admin.setUsername(username);
        admin.setPassword(password);
       int s= adminService.register(admin);
       if(s == 0){
           return "user/registerfail";
       }
       if(s==1){
           System.out.println("注册成功");
       }
      return "user/registersucess";


    }

    @PostMapping("/login01")
    @ResponseBody
    public Result<UmsAdmin> login01(Model model, String username, String password) {
        UmsAdmin admin = new UmsAdmin();
        admin.setUsername(username);
        admin.setPassword(password);

        Result<UmsAdmin> login01 = this.adminService.login01(admin);



        return login01;
    }



}
